Expand description
Simple and fast lockless async channels
Lightweight async channel that can be used to implement futures, streams, notifiers, and actors.
Whisk defines a simple Channel
type rather than splitting into sender /
receiver pairs. A Channel
can both send and receive.
Optional Features
- futures-core: Implement
Stream
forChannel
(genericT
must beOption<Item>
) - pasts: Implement
Notifier
forChannel
Getting Started
use whisk::Channel;
enum Cmd {
/// Tell messenger to add
Add(u32, u32, Channel<u32>),
}
async fn worker_main(commands: Channel<Option<Cmd>>) {
while let Some(command) = commands.recv().await {
println!("Worker receiving command");
match command {
Cmd::Add(a, b, s) => s.send(a + b).await,
}
}
println!("Worker stopping…");
}
async fn tasker_main() {
// Create worker on new thread
println!("Spawning worker…");
let channel = Channel::new();
let worker_task = worker_main(channel.clone());
let worker_thread =
std::thread::spawn(|| pasts::Executor::default().block_on(worker_task));
// Do an addition
println!("Sending command…");
let oneshot = Channel::new();
channel.send(Some(Cmd::Add(43, 400, oneshot.clone()))).await;
println!("Receiving response…");
let response = oneshot.recv().await;
assert_eq!(response, 443);
// Tell worker to stop
println!("Stopping worker…");
channel.send(None).await;
println!("Waiting for worker to stop…");
worker_thread.join().unwrap();
println!("Worker thread joined");
}
fn main() {
// Call into executor of your choice
pasts::Executor::default().block_on(tasker_main());
}
Structs
- An MPMC channel with both send and receive capabilities
- A
Queue
can send messages to itself, and can be shared between threads and tasks.